package com.shine.ws.client.util;

import java.util.List;
import javax.xml.namespace.QName;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * @Description:
 * @CreateTime: 2023/7/17 8:45
 * @Version:
 * @Since: 1.8
 * @Author: Forward Seen
 */
public class ClientLoginInterceptor extends AbstractPhaseInterceptor<SoapMessage> {
    private String username;
    private String password;

    public ClientLoginInterceptor(String username, String password) {
        super(Phase.PREPARE_SEND);
        this.username = username;
        this.password = password;
    }

    @Override public void handleMessage(SoapMessage message) throws Fault {
        List<Header> headers = message.getHeaders();//获取全部头信息
        Document doc = DOMUtils.createDocument();//创建文档
        Element auth = doc.createElement("authority");//认证数据信息
        Element username = doc.createElement("username");
        Element password = doc.createElement("password");
        username.setTextContent(this.username);
        password.setTextContent(this.password);
        auth.appendChild(username);
        auth.appendChild(password);
        headers.add(0, new Header(new QName("authority"), auth));
    }
}